package org.edu.modules.sxjy.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.sxjy.entity.VeAttendance;
import org.edu.modules.sxjy.entity.VeCommon;
import org.edu.modules.sxjy.entity.VeStudent;
import org.edu.modules.sxjy.exception.MyException;
import org.edu.modules.sxjy.service.IVeAttendanceService;
import org.edu.modules.sxjy.service.IVeStudentService;
import org.edu.modules.sxjy.utils.PageResponseResult;
import org.edu.modules.sxjy.utils.PagingUtils;
import org.edu.modules.sxjy.vo.VeAttendanceAddVo;
import org.edu.modules.sxjy.vo.VeAttendanceInfoVo;
import org.edu.modules.sxjy.vo.VeAttendanceVo;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@Api(tags={"学生实习考勤记录查看"})
@RestController
@RequestMapping({"/sxjy/veAttendance"})
@Component
@ApiSort(60)
public class VeAttendanceController
        extends BaseController<VeAttendance, IVeAttendanceService>
{
    private static final Logger log = LoggerFactory.getLogger(VeAttendanceController.class);
    @Autowired
    private IVeAttendanceService veAttendanceService;
    @Autowired
    private IVeStudentService veStudentService;

    @AutoLog("学生实习考勤记录查看-导出查看信息数据")
    @ApiOperation(value="导出查看信息数据", notes="学生实习考勤记录查看-导出查看信息数据")
    @PostMapping({"/exportXls"})
    public ModelAndView exportXls(@RequestBody VeCommon veCommon)
            throws MyException
    {
        List<VeAttendanceInfoVo> veAttendanceInfoVoList = this.veAttendanceService.getOneInfoXls(veCommon);

        ModelAndView modelAndView = new ModelAndView(new JeecgEntityExcelView());

        modelAndView.addObject("fileName", "学生实习考勤记录");
        modelAndView.addObject("entity", VeAttendanceInfoVo.class);
        LoginUser user = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        modelAndView.addObject("params", new ExportParams("学生实习考勤记录", "导出人:" + user.getRealname(), "学生实习考勤记录"));
        if ((veAttendanceInfoVoList != null) && (veAttendanceInfoVoList.size() > 0)) {
            modelAndView.addObject("data", veAttendanceInfoVoList);
        } else {
            throw new MyException("当前文件无数据");
        }
        return modelAndView;
    }

    @AutoLog("学生实习考勤记录查看-查询列表")
    @ApiOperation(value="查询列表", notes="学生实习考勤记录查看-查询列表")
    @GetMapping({"/selectAll"})
    public PageResponseResult selectAll(VeAttendanceVo veAttendanceVo, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, VeCommon veCommon)
    {
        Integer startLine = Integer.valueOf((pageNo.intValue() - 1) * pageSize.intValue());
        List<VeAttendanceVo> veAttendanceVoList = this.veAttendanceService.selectAll(veAttendanceVo, startLine, pageSize, veCommon);
        int total = this.veAttendanceService.getSumPage(veAttendanceVo, veCommon);
        if ((Objects.isNull(veAttendanceVoList)) && (veAttendanceVoList.size() <= 0)) {
            return new PageResponseResult(Integer.valueOf(1), 1, 0, Result.error("未找到对应数据"));
        }
        double totals = total;
        double pageSizes = pageSize.intValue();
        double totalPage = Math.ceil(totals / pageSizes);
        return new PageResponseResult(pageNo, (int)totalPage, total, Result.OK(veAttendanceVoList));
    }

    @AutoLog("学生实习考勤记录查看-查看详细")
    @ApiOperation(value="查看详细", notes="学生实习考勤记录查看-查看详细")
    @GetMapping({"/selectOneInfo"})
    public PageResponseResult selectOneInfo(int id, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, @RequestParam(name="userId", required=false) String userId, int type)
    {
        List<VeAttendanceInfoVo> veAttendanceInfoVoList = this.veAttendanceService.getOneInfo(Integer.valueOf(id), userId, Integer.valueOf(type));
        if ((veAttendanceInfoVoList != null) && (veAttendanceInfoVoList.size() > 0))
        {
            Map<String, Object> pageData = PagingUtils.getPageData(veAttendanceInfoVoList, pageNo, pageSize);
            List<VeAttendanceInfoVo> veAttendanceInfoVoLists = (List)pageData.get("data");
            int totalPage = ((Integer)pageData.get("totalPage")).intValue();
            return new PageResponseResult(pageNo, totalPage, veAttendanceInfoVoList.size(), Result.OK(veAttendanceInfoVoLists));
        }
        return new PageResponseResult(Integer.valueOf(1), 1, 0, Result.OK());
    }

    @AutoLog("学生实习考勤记录查看-查看详细")
    @ApiOperation(value="查看详细", notes="学生实习考勤记录查看-查看详细")
    @GetMapping({"/selectStuInfo"})
    public PageResponseResult selectStuInfo(String userId, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, int typeId)
    {
        List<VeAttendanceInfoVo> veAttendanceInfoVoList = this.veAttendanceService.selectStuInfo(userId, typeId);
        if (veAttendanceInfoVoList != null)
        {
            Map<String, Object> pageData = PagingUtils.getPageData(veAttendanceInfoVoList, pageNo, pageSize);
            List<VeAttendanceInfoVo> veAttendanceInfoVoLists = (List)pageData.get("data");
            int totalPage = ((Integer)pageData.get("totalPage")).intValue();
            return new PageResponseResult(pageNo, totalPage, veAttendanceInfoVoList.size(), Result.OK(veAttendanceInfoVoLists));
        }
        return new PageResponseResult(Integer.valueOf(1), 1, 0, Result.OK());
    }

    @AutoLog("学生实习考勤记录查看-新增信息")
    @ApiOperation(value="新增信息", notes="学生实习考勤记录查看-新增信息")
    @PostMapping({"/add"})
    public Result<?> add(@RequestBody VeAttendanceAddVo veAttendanceAddVo)
    {
        return this.veAttendanceService.saves(veAttendanceAddVo);
    }

    @AutoLog("学生实习考勤记录查看-根据id查询学生名称学号")
    @ApiOperation(value="根据id查询学生名称学号", notes="学生实习考勤记录查看-根据id查询学生名称学号")
    @GetMapping({"/getStuXmXh"})
    public Result<?> selectAll(int id)
    {
        if (id != 0)
        {
            VeStudent veStudent = (VeStudent)this.veStudentService.getById(Integer.valueOf(((VeAttendance)this.veAttendanceService.getById(Integer.valueOf(id))).getStuUserId()));
            return Result.ok(veStudent);
        }
        return Result.error("学生信息有误");
    }

    @AutoLog("学生实习考勤记录查看-通过id删除")
    @ApiOperation(value="通过id删除", notes="学生实习考勤记录查看-通过id删除")
    @DeleteMapping({"/delete"})
    public Result<?> delete(@RequestParam(name="id", required=true) String id)
    {
        this.veAttendanceService.removeById(id);
        return Result.OK("删除成功!");
    }

    @AutoLog("学生实习考勤记录查看-批量删除")
    @ApiOperation(value="批量删除", notes="学生实习考勤记录查看-批量删除")
    @DeleteMapping({"/deleteBatch"})
    public Result<?> deleteBatch(@RequestParam(name="ids", required=true) String ids)
    {
        this.veAttendanceService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }
}
